package com.etc.service.impl;

import com.etc.dao.ProvinceDao;
import com.etc.entity.Area;
import com.etc.entity.City;
import com.etc.entity.Province;
import com.etc.redis.ProvCityAreaKey;
import com.etc.redis.RedisService;
import com.etc.service.ProvinceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 文件名:com.etc.service.impl.ProvinceServinceImpl
 * 描述：省份业务
 * 作者:吴仕泉
 * 时间:2019/5/26 16:07
 */
@Service
public class ProvinceServinceImpl implements ProvinceService {

    @Autowired
    RedisService redisService;

    @Autowired
    ProvinceDao provinceDao;

    @Autowired
    AreaServiceImpl areaService;

    @Autowired
    CityServiceImpl cityService;

    @Override
    public List<Province> findProvinces() {
        //从缓存中读取所需的省份信息
        List<Province> provinces = redisService.getList(ProvCityAreaKey.allProvinceKey,"001");
        //判断缓存中是否存在该信息时
        if(provinces == null){
            //从数据库中读取省份信息
            provinces = provinceDao.listSelectAllProvince();

            if(provinces==null){
                return null;
            }
            //保存在缓存池中
            redisService.setList(ProvCityAreaKey.allProvinceKey,"001",provinces);
        }

        return  provinces;
    }

    //根据省份编号查询省份的信息
    @Override
    public Province findProvinceByProvinceNum(Integer provNum) {
        Province province = redisService.get(ProvCityAreaKey.oneProvinceKey,String.valueOf(provNum),Province.class);
        if(province == null){
            province  = provinceDao.getProvinceMessageByProvinceNum(provNum.longValue());
            if(province != null){
                redisService.set(ProvCityAreaKey.oneProvinceKey,String.valueOf(provNum),province);
            }
        }
        return province;
    }

    //收货地址的拼接
    @Override
    public String receipAddress(Integer provId, Integer cityId, Integer areaId, String address) {
        Province province = findProvinceByProvinceNum(provId);

        City city = cityService.findCityByCityNum(cityId);

        Area area = areaService.findAreaByAreaNum(areaId);
        if(province == null || city == null ||area == null||address==null){
            return null;
        }
        StringBuffer sb = new StringBuffer();
        sb.append(province.getProvincename());
        sb.append(city.getCityname());
        sb.append(area.getAreaname());
        sb.append(address);
         return sb.toString();
    }
}
